(function() {

	function IsEmpty(fData) {
		var trimStr = null;
		if ((fData == null) || (fData.length == 0)) {
			return true;
		}
		if (fData != null) {
			trimStr = jQuery.trim(fData);
			if (trimStr.length <= 0) {
				return true;
			}
		}
		return false;
	}

	function getQueryStringArgs(key) {
		var qs = (location.search.length > 0 ? location.search.substring(1) : '');
		var items = qs.length ? qs.split("&") : [];
		var item = null;
		var name = null;
		var value = null;
		for (var i = 0; i < items.length; i++) {
			item = items[i].split("=");
			name = decodeURIComponent(item[0]);
			value = decodeURIComponent(item[1]);
			if (name.length && name == key) {
				return value;
			}
		}
		return '';
	}


	$.get('../userFun/find/menu', function(message) {
		if (IsEmpty(message)) {
			artError('服务器忙,请联系系统管理员！');
			return;
		}
		if (message.state === 0) {
			generateMenu(message);
		} else if (message.state == 1) {
			artError(message.msg);
		} else {
			checkStateShowErrorMsgAndDealResult(JSON.stringify(message));
		}
	}, 'json');

	function generateMenu(rst) {
		var key = [];
		var parent = [];
		var sub = [];
		var data = rst.result.data;
		var len = rst.result.len;
		var i = 0;
		for (; i < len; i++) {
			var menu = data[i];
			var id = Number(menu[0]);
			var pid = Number(menu[2]);
			if (pid == 0) {
				if (parent[id] == null) {
					parent[id] = menu;
					key.push(id);
				}
			} else {
				if (sub[pid] == null) {
					sub[pid] = [];
				}
				sub[pid].push(menu);
			}
		}
		//生成 HTML 代码
		$div = $("<div id='submenu' class='container_L fl'></div>");

		for ( i = 0; i < key.length; i++) {
			//"funId", "funName", "parentId", "url", "style"
			var j = key[i];
			var h3 = "<h3><span class='" + parent[j][4] + "'></span>" + parent[j][1] + "<i></i></h3>";
			$div.append($(h3));

			var subs = sub[j];
			var $ul = $("<ul id='" + parent[j][0] + "'></ul>");
			$div.append($ul);
			for (var k = 0; typeof subs !== "undefined" && k < subs.length; k++) {
				var url = subs[k][3];
				if (url == null) {
					url = 'template.html';
				} else {
					url += "?menu=" + parent[j][0] + "&submenu=" + subs[k][0];
				}
				var li = "<li id='" + subs[k][0] + "'><a href='" + url + "'>" + subs[k][1] + "</a></li>";
				$ul.append($(li));
			}
		}
		//高亮菜单
		$("#container").prepend($div);
		_menu = getQueryStringArgs('menu');
		_subMenu = getQueryStringArgs('submenu');
		$("#" + _menu).show();
		$("#" + _subMenu).addClass("focus");
		init();
	}

	function init() {
		for (var i = 0; i < $("#submenu li").length; i++) {
			if ($("#submenu li").eq(i).hasClass("focus")) {
				$("#submenu li").eq(i).parent().show();
				$("#submenu li").eq(i).parent().prev("h3").children("i").addClass("info1");
			}
		}//有当前栏目的父菜单展示
		$("#submenu>h3").click(function() {
			var $sub = $(this), $span = $(this).children("span").eq(0), //第一个背景
			$i = $(this).children("i").eq(0), //第二个背景
			$ul = $(this).next("ul"), $li = $ul.children("li");
			if ($ul.is(":visible")) {
				$i.attr("class", "");
				for (var i = 0; i < $li.length; i++) {
					if ($li.eq(i).hasClass("focus")) {
						$span.css("background-position-y") ? $span.css("background-position-y", (parseInt($span.css("background-position-y").split("px")[0]) - 429 + "px").toString()) : $span.css("background-position", "0px " + (parseInt($span.css("background-position").split("px")[1]) - 429 + "px").toString());
						$i.attr("class", "info2");
						$sub.addClass("focus");
					}
				}
				$ul.hide();
			} else {
				$i.attr("class", "info1");
				if ($sub.hasClass("focus")) {
					$sub.removeClass("focus");
				}
				for (var i = 0; i < $li.length; i++) {
					if ($li.eq(i).hasClass("focus")) {
						$span.css("background-position-y") ? $span.css("background-position-y", (parseInt($span.css("background-position-y").split("px")[0]) + 429 + "px").toString()) : $span.css("background-position", "0px " + (parseInt($span.css("background-position").split("px")[1]) + 429 + "px").toString());
					}
				}
				$ul.show();
			}
		});
	setContainer();
	$("#submenu").children("h3").click(function() {
		setContainer();
	});
	}

})();
function showTab(column, tab) {
}
